package Servlet.Teacher;

import Entity.Role.Admin;
import Entity.Role.Student;
import Entity.Role.Teacher;
import Service.Teacher.Imp.LoginServiceImp;
import Service.Teacher.LoginService;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by IntelliJ IDEA.
 * Author: Deng PengFei
 * Date: 2021/8/20
 * Time: 19:49
 */

@WebServlet(urlPatterns = {"/stuLogin", "/teacherLogin", "/adminLogin", "/logout", "/getInfo", "/updatePass"}, name = "LoginServlet")
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String url = request.getRequestURI();
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        if ("/stuLogin".equals(url)) {
            Student student = new Student();
            student.setSNO(userName);
            student.setPassword(password);
            stuLogin(request, response, student);
        } else if ("/teacherLogin".equals(url)) {
            Teacher teacher = new Teacher();
            teacher.setTNO(userName);
            teacher.setPassword(password);
            teacherLogin(request, response, teacher);
        } else if ("/adminLogin".equals(url)) {
            Admin admin = new Admin();
            admin.setAdminName(userName);
            admin.setAdminPassword(password);
            adminLogin(request, response, admin);
        } else if ("/logout".equals(url)) {
            logout(request, response);
        } else if ("/getInfo".equals(url)) {
            getInfo(request, response);
        } else if ("/updatePass".equals(url)) {
            updatePass(request, response);
        }
    }

    private void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
        LoginService loginService = new LoginServiceImp();
        HttpSession session = request.getSession();
        String NO = (String) session.getAttribute("userName");
        loginService.removeUserLogin(NO);
        session.invalidate();
        response.sendRedirect("login.jsp");
    }


    /**
     * 修改密码
     */
    private void updatePass(HttpServletRequest request, HttpServletResponse response) throws IOException {
        LoginService loginService = new LoginServiceImp();
        String NO = request.getParameter("userName");
        String role = request.getParameter("role");
        String oldPass = request.getParameter("oldPass");
        String newPass = request.getParameter("newPass");
        Integer code = loginService.updatePassByNOAndRole(NO, role, oldPass, newPass);
        if (code == 1001) { //如果密码修改成功 则需要重新登录
            HttpSession session = request.getSession();
            session.invalidate();
        }
        PrintWriter out = response.getWriter();
        out.print(code);
        out.close();
    }

    /**
     * 个人信息页面获取信息
     */
    private void getInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        LoginService loginService = new LoginServiceImp();
        String NO = request.getParameter("userName");
        String role = request.getParameter("role");
        Object object = loginService.getInfoByNOAndRole(NO, role);
        String json = JSONObject.toJSONString(object);
        PrintWriter out = response.getWriter();
        out.print(json);
        out.close();
    }


    /**
     * 学生登录
     *
     * @param request  请求
     * @param response 响应
     * @param student  登录的学生对象
     * @throws IOException 异常
     */
    protected void stuLogin(HttpServletRequest request, HttpServletResponse response, Student student) throws IOException {
        PrintWriter out = response.getWriter();
        LoginService loginService = new LoginServiceImp();
        int code = loginService.stuLogin(student);
        if (code == 1001) {
            HttpSession session = request.getSession();
            session.setAttribute("userName", student.getSNO());
        }
        out.print(code);
        out.close();
    }

    /**
     * 老师登录
     *
     * @param request  请求
     * @param response 响应
     * @param teacher  老师的学生对象
     * @throws IOException 异常
     */
    protected void teacherLogin(HttpServletRequest request, HttpServletResponse response, Teacher teacher) throws IOException {
        PrintWriter out = response.getWriter();
        LoginService loginService = new LoginServiceImp();
        int code = loginService.teacherLogin(teacher);
        if (code == 1001) {
            HttpSession session = request.getSession();
            session.setAttribute("userName", teacher.getTNO());
        }
        out.print(code);
        out.close();
    }

    /**
     * 管理员登录
     *
     * @param request  请求
     * @param response 响应
     * @param admin    登录的管理员对象
     * @throws IOException 异常
     */
    protected void adminLogin(HttpServletRequest request, HttpServletResponse response, Admin admin) throws IOException {
        PrintWriter out = response.getWriter();
        LoginService loginService = new LoginServiceImp();
        int code = loginService.adminLogin(admin);
        if (code == 1001) {
            HttpSession session = request.getSession();
            session.setAttribute("userName", admin.getAdminName());
        }
        out.print(code);
        out.close();
    }


}
